package org.xwalk.core;

import android.net.Uri;
import java.util.Map;

import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;



public class XWalkWebResourceRequestHandler   implements XWalkWebResourceRequest  {

    private ArrayList<Object> constructorTypes;
    private ArrayList<Object> constructorParams;
    private ReflectMethod postWrapperMethod;


    private XWalkCoreWrapper coreWrapper;
    private Object bridge;

    protected Object getBridge() {
        return bridge;
    }

    public XWalkWebResourceRequestHandler(Object bridge) {
        this.bridge = bridge;
        reflectionInit();
    }


    public Uri getUrl() {
        try {
            return (Uri)getUrlMethod.invoke();
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        return null;
    }

    private ReflectMethod getUrlMethod = new ReflectMethod(null, "getUrl");


    public boolean isForMainFrame() {
        try {
            return (Boolean)isForMainFrameMethod.invoke();
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        return false;
    }

    private ReflectMethod isForMainFrameMethod = new ReflectMethod(null, "isForMainFrame");


    public boolean hasGesture() {
        try {
            return (Boolean)hasGestureMethod.invoke();
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        return false;
    }

    private ReflectMethod hasGestureMethod = new ReflectMethod(null, "hasGesture");


    public String getMethod() {
        try {
            return (String)getMethodMethod.invoke();
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        return null;
    }

    private ReflectMethod getMethodMethod = new ReflectMethod(null, "getMethod");


    public Map<String, String> getRequestHeaders() {
        try {
            return (Map<String, String>)getRequestHeadersMethod.invoke();
        } catch (UnsupportedOperationException e) {
            if (coreWrapper == null) {
                throw new RuntimeException("Crosswalk's APIs are not ready yet");
            } else {
                XWalkCoreWrapper.handleRuntimeError(e);
            }
        }
        return null;
    }

    private ReflectMethod getRequestHeadersMethod = new ReflectMethod(null, "getRequestHeaders");


    void reflectionInit() {
        XWalkCoreWrapper.initEmbeddedMode();

        coreWrapper = XWalkCoreWrapper.getInstance();
        if (coreWrapper == null) {
            XWalkCoreWrapper.reserveReflectObject(this);
            return;
        }

        getUrlMethod.init(bridge, null,
                "getUrlSuper");
        isForMainFrameMethod.init(bridge, null,
                "isForMainFrameSuper");
        hasGestureMethod.init(bridge, null,
                "hasGestureSuper");
        getMethodMethod.init(bridge, null,
                "getMethodSuper");
        getRequestHeadersMethod.init(bridge, null,
                "getRequestHeadersSuper");
    }

}
